import axios from 'axios'
import Storage from './storage'
//import _ from 'lodash'

let instance = axios.create();

// 覆写库的超时默认值
// 现在，在超时前，所有请求都会等待5分钟
instance.defaults.timeout = 1000 * 60 * 60;

// 请求的拦截
instance.interceptors.request.use(function (request) {
    let token = Storage.getItem("token")
    request.headers["accept"] = "application/json; charset=utf-8"
    request.headers["access-token"] = token
    request.headers["contentType"] = "application/json"
    request.headers["dataType"] = "json"
    return request;
});

// 响应的拦截
instance.interceptors.response.use(function (response) {
    return response;
});

const urlData = {
    url: '',
    data: {},
    autoLogin: false
}

const getPrefix = () => {
    let network = Storage.getObject('network')
    let selectedIndex = Storage.getItem('selectedIndex')
    let locationNet = {
        ip: window.location.hostname,
        port: window.location.port,
    }
    let prefix = network ? network[selectedIndex] : locationNet
    prefix = process.env.NODE_ENV === 'development' ? '/test' : ((prefix.protocol ? prefix.protocol : 'http') + '://' + prefix.ip + (prefix.port ? ':' + prefix.port : '') + (prefix.appName ? '/' + prefix.appName : ''))
    return prefix
}

const formateUrl = (controllerName) => {
    let loginConfig = Storage.getObject("loginConfig")
    let multipleObj = Storage.getObject("multipleObj")
    // 是否启用多工作中心  false 不启用, true 启用
    let datarole = multipleObj ? "&_dr=" + multipleObj.workCenterGid : ''
    if (!loginConfig) {
        return 'no login'
    } else if (!controllerName) {
        return 'no controllerName'
    }
    let url = getPrefix() + "autoLoginController!login.m?_u=" + loginConfig.un + "&_p=" + loginConfig.pw +
        datarole + "&_pt=min" + "&_to=" + controllerName;
    //window.console.log(url)
    return url
}

const get = (params = urlData) => {
    window.console.log('get', params)
    params = Object.assign(urlData, params)
    let url = getPrefix() + params.url
    return instance.get(url, {
        params: params.data
    })
}

const post = (params = urlData) => {
    params = Object.assign(urlData, params)
    let url = params.autoLogin ? formateUrl(params.url) : getPrefix() + params.url
    //window.console.log(params, url)
    try {
        if (params.data instanceof FormData && params.data.keys()) { //FormData格式参数
            let form = new FormData();
            for (let key of params.data.keys()) {
                //字符串类型参数特殊符号需要进行处理
                let val = params.data.get(key);
                if (val != null && Object.prototype.toString.call(val) === "[object String]") {
                    //params.data.delete(key);
                    val = val.replace(/%/g, "%25").replace(/\+/g, "%2B");
                }
                form.append(key, val);
            }
            params.data = form;
        } else if (Object.prototype.toString.call(params.data) === "[object String]") { //字符串类型参数特殊符号需要进行处理
            params.data = params.data.replace(/%/g, "%25").replace(/\+/g, "%2B");
        } else {
            for (let i in params.data) {
                //字符串类型参数特殊符号需要进行处理
                if (params.data[i] && Object.prototype.toString.call(params.data[i]) === "[object String]") {
                    params.data[i] = params.data[i].replace(/%/g, "%25").replace(/\+/g, "%2B");
                }
            }
        }
    } catch (e) {
        console.log('捕获到异常：', e);
    }
    return instance.post(url, params.data, params.config)
}


const url = {
    getAppFirstPageInfo: 'productPadCommController!getAppFirstPageInfo.m', // 首页顶部轮播信息
    getAppMsg: 'productPadCommController!getAppMsg.m', // 获取消息
    getAllWorkCenterAndCell: 'productPadCommController!getAllWorkCenterAndCellByLoginName.m', //
    changePassword: 'productPadCommController!doChangePwd.m'
}

export default {
    url,
    get,
    post,
    // getUrl,
    instance,
    formateUrl,
    getPrefix
}